#include "calibrationwidget.h"
#include "ui_calibrationwidget.h"
#include "common/common.h"
#include <QDateTime>
#include "common/tsettings/tsettings.h"

const char* CalibrationWidget::CALIDATETIME = "calidatetime";
const char* CalibrationWidget::CALIWEIGHT = "caliweight";

CalibrationWidget::CalibrationWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CalibrationWidget)
{
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setWindowState(Qt::WindowMaximized);
    ui->setupUi(this);
    ui->widget->hide();
    ui->inputkey->setEditor(ui->lab_calw);
    ui->inputkey->setDeci(1);
    ui->inputkey->setMaxLenth(5);
    ui->inputkey->hideEscOk();
    ui->lab_unit->setText("kg"/*SPSCALEADAPTOR()->getUnit()*/);
    mStep = 1;
    stepWidgetDisabled(1,false);
    stepWidgetDisabled(2,true);
    stepWidgetDisabled(3,true);
    stepWidgetDisabled(4,true);
    mTimer = new QTimer(this);
    connect(mTimer,SIGNAL(timeout()),this,SLOT(onTimeOut()));
    ui->lab_st->setStyleSheet("QLabel{color:rgb(218,218,218);}");
    ui->lab_datetime->setText(getCaliDatetime());
    ui->lab_net->setText(getAdw());
}

CalibrationWidget::~CalibrationWidget()
{
    delete ui;
}

QString CalibrationWidget::getAdw()
{
    TSettings set(DIR_SETTINGS,TSettings::IniFormat);
    return set.value(CALIWEIGHT,SPSCALEADAPTOR()->getMax()).toString();
}

QString CalibrationWidget::getCaliDatetime()
{
    TSettings set(DIR_SETTINGS,TSettings::IniFormat);
    return set.value(CALIDATETIME,QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).toString();
}

void CalibrationWidget::on_btn_back1_clicked()
{
    this->deleteLater();
}

void CalibrationWidget::on_btn_start_clicked()
{
    switch (mStep) {
    case 1:
        SPSCALEADAPTOR()->writeCmd("ADZ");
        stepWidgetDisabled(1,true);
        stepWidgetDisabled(2,false);
        ui->btn_start->setText("下一步");
        break;
    case 2:
        stepWidgetDisabled(2,true);
        stepWidgetDisabled(3,false);
         mTimer->start(200);
        break;
    case 3:
        stepWidgetDisabled(3,true);
        stepWidgetDisabled(4,false);
        mTimer->stop();
        ui->btn_start->setText("完成");
        break;
    case 4:        
    {
        SPSCALEADAPTOR()->writeCmd(QString("LOAD%1").arg(ui->lab_calw->text().toFloat()));
        stepWidgetDisabled(1,true);
        stepWidgetDisabled(2,true);
        stepWidgetDisabled(3,true);
        stepWidgetDisabled(4,true);
        ui->btn_start->setText("结束");
        QString datetime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
        QString wgt = /*SPSCALEADAPTOR()->stringNet()*/ui->lab_calw->text()+"kg";
        ui->lab_datetime->setText(datetime);
        ui->lab_net->setText(SPSCALEADAPTOR()->stringNet()+SPSCALEADAPTOR()->getUnit());
        TSettings set(DIR_SETTINGS,TSettings::IniFormat);
        set.setValue(CALIDATETIME,datetime);
        set.setValue(CALIWEIGHT,wgt);
        //SPSCALEADAPTOR()->setAdw(SPSCALEADAPTOR()->floatNet());
        break;
    }
    case 5:
        ui->btn_start->setText("开始");
        SPSCALEADAPTOR()->syncScalePara();
        stepWidgetDisabled(1,false);
        ui->lab_st->setStyleSheet("QLabel{color:rgb(218,218,218);}");
        break;
    default:
        break;
    }
    mStep++;
    if(mStep>5) mStep = 1;
}

void CalibrationWidget::onTimeOut()
{
    if(!SPSCALEADAPTOR()->zeroFlag())
    {qDebug()<<"NOT ZERO";
        if(SPSCALEADAPTOR()->stabFlag())
        {
            ui->lab_st->setStyleSheet("QLabel{color:rgb(255,0,0);}");
        }
        else
        {
            ui->lab_st->setStyleSheet("QLabel{color:rgb(218,218,218);}");
        }
    }
}

void CalibrationWidget::stepWidgetDisabled(int index, bool disable)
{
    switch (index) {
    case 1:
        ui->wdt1->setDisabled(disable);
        break;
    case 2:
        ui->wdt2->setDisabled(disable);
        ui->inputkey->setEditor(disable?NULL:ui->lab_calw);
        break;
    case 3:
        ui->wdt3->setDisabled(disable);
        break;
    case 4:
        ui->wdt4->setDisabled(disable);
        break;
    default:
        break;
    }
}
